//
//  PersonSelectViewFlowLayout.swift
//  App_Vision
//
//  Created by Flutter on 2021/8/6.
//

import UIKit

class PersonSelectViewFlowLayout: UICollectionViewFlowLayout {
    
    var isZoom: Bool = false
    
}


// MARK: - Override
extension PersonSelectViewFlowLayout {
    
    
    /*  多次调用 只要滑出范围就会 调用
     *  当CollectionView的显示范围发生改变的时候，是否重新发生布局
     *  一旦重新刷新 布局，就会重新调用
     *  1.layoutAttributesForElementsInRect：方法
     *  2.preparelayout方法
     */
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    
    override func prepare() {
        super.prepare()
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        let temps:[UICollectionViewLayoutAttributes]? = super.layoutAttributesForElements(in: rect)?.map({$0.copy()}) as? [UICollectionViewLayoutAttributes]
        
        guard self.isZoom else { return temps }
        let centerX = (collectionView?.contentOffset.x ?? 0) + (collectionView?.bounds.size.width ?? 0) * 0.5
        return temps?.map({ attributes in
            let centerDistance =  fabsf(Float(attributes.center.x - centerX))
            let scale = CGFloat(1.0 / (1 + centerDistance * 0.003))
            attributes.transform = CGAffineTransform(scaleX: scale, y: scale)
            attributes.alpha = scale
            return attributes
        })
    }
    
    
    
}
